package com.ipbd.controller;

import com.ipbd.dto.LoginFormDTO;
import com.ipbd.service.AppUserService;
import com.ipbd.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private AppUserService appUserService;

    /**
     * 用户通过验证码登录
     * @return
     */
    @PostMapping("/loginWithCode")
    public R loginWithCode(@RequestBody LoginFormDTO loginForm, HttpSession session) {
        return appUserService.loginWithCode(loginForm, session);
    }

    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @PostMapping("/sendCode")
    public R sendCode(@RequestParam("phone") String phone, HttpSession session) {
        return appUserService.sendCode(phone, session);
    }

    /**
     * 使用密码登录
     * @param loginFormDTO
     * @return
     */
    @PostMapping("loginWithPassword")
    public R loginWithPassword(@RequestBody LoginFormDTO loginFormDTO) {
        return appUserService.loginWithPassword(loginFormDTO);
    }

    /**
     * 用户通过密码登录
     * @param phone
     * @return
     */
    @PostMapping("/updateCode")
    public R sendUpdateCode(@RequestParam("phone") String phone) {
        return appUserService.sendUpdateCode(phone);
    }

    /**
     * 修改密码
     * @return
     */
    @PutMapping("/updatePwd")
    private R updatePassword(@RequestBody LoginFormDTO loginForm, HttpSession session) {
        return appUserService.updatePassword(loginForm, session);
    }

    /**
     * 设置密码
     * @param loginFormDTO
     * @return
     */
    @PostMapping("/setPwd")
    private R setInitialPassword(@RequestBody LoginFormDTO loginFormDTO) {
        return appUserService.setInitialPassword(loginFormDTO);
    }

    /**
     * 登出
     * @return
     */
    @GetMapping("/logout")
    private R logout() {
        UserHolder.removeUser();
        return R.ok();
    }
}
